package cool.tianyou.blue.bird.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.BatteryManager;
import android.os.Build;

import androidx.annotation.RequiresApi;

import cool.tianyou.blue.bird.factory.MessageFactory;
import cool.tianyou.blue.bird.model.CommonMessage;
import cool.tianyou.blue.bird.model.NotificationData;
import cool.tianyou.blue.bird.util.Util;

import java.util.Date;
import java.util.HashMap;
import java.util.Objects;

public class BatteryReceiver extends BroadcastReceiver {
    private Integer level;
    private Boolean isCharging;

    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    public void onReceive(Context context, Intent intent) {
        sendBatteryData(context, intent,false);

    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    public void sendBatteryData(Context context, Intent intent, boolean always) {
        int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
        int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
        boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||
                status == BatteryManager.BATTERY_STATUS_FULL;
        if (!always && Objects.equals(level, this.level) && Objects.equals(this.isCharging, isCharging)) {
            return;
        }
        this.level = level;
        this.isCharging = isCharging;
        Util.sendMessage(MessageFactory.batteryMessage(level, isCharging), context);
    }
}
